package com.relytosoft.mizar.analyze.system.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.AyGroupBaseDto;
import com.relytosoft.mizar.analyze.system.api.entity.AyGroupBase;
import com.relytosoft.mizar.analyze.system.api.pojo.vo.AyGroupBaseVo;
import com.relytosoft.mizar.analyze.system.services.service.AyGroupBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author lj
 * @date 2022/5/12 11:55
 */
@Api(tags = {"库"})
@RestController
@RequestMapping("/system/group")
public class AyGroupBaseController {

    @Resource
    private AyGroupBaseService ayGroupBaseService;


    @ApiOperation("库列表查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(@RequestParam("condition") String condition,
                                                    @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyGroupBaseDto ayGroupBaseDto = JSONObject.parseObject(condition, AyGroupBaseDto.class);
        AyGroupBase ayGroupBase = BeanCopyUtils.copy(ayGroupBaseDto, AyGroupBase.class);
        IPage<AyGroupBase> page = ayGroupBaseService.selectAyGroupBasePageList(pageQuery, ayGroupBase);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("新增库")
    @PostMapping({"/add"})
    public ResponseResult<Void> add(@RequestBody AyGroupBaseDto ayGroupBaseDto) {
        AyGroupBase ayGroupBase = BeanCopyUtils.copy(ayGroupBaseDto, AyGroupBase.class);
        boolean b = ayGroupBaseService.addAyGroupBase(ayGroupBase);
        if (b) {
            return ResponseResult.ok("新增库成功");
        } else {
            return ResponseResult.fail("新增库失败");
        }
    }

    @ApiOperation("根据ID查询")
    @GetMapping({"{groupId}"})
    public ResponseResult<AyGroupBaseVo> id(@PathVariable Integer groupId) {
        return ResponseResult.ok(ayGroupBaseService.findById(groupId));
    }


    @ApiOperation("删除库")
    @DeleteMapping("del")
    public ResponseResult<Void> remove(@RequestBody List<Integer> list) {
        ArrayList<String> plateformGroupIds = ayGroupBaseService.updateStatus(list);
        if (plateformGroupIds != null && !plateformGroupIds.isEmpty()) {
            ayGroupBaseService.sendRemoveToKafka(plateformGroupIds);
            return ResponseResult.ok("删除人脸库成功");
        }
        if (plateformGroupIds != null) {
            return ResponseResult.ok("删除车辆库成功");
        } else {
            return ResponseResult.fail("删除失败");
        }
    }

    @ApiOperation("修改库")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody AyGroupBaseDto ayGroupBaseDto) {
        AyGroupBase ayGroupBase = BeanCopyUtils.copy(ayGroupBaseDto, AyGroupBase.class);
        boolean b = ayGroupBaseService.updateAyGroupBase(ayGroupBase);
        if (b) {
            return ResponseResult.ok("修改成功");
        } else {
            return ResponseResult.fail("修改失败");
        }
    }


    @ApiOperation("根据ID批量查询库信息")
    @PostMapping("batch")
    public ResponseResult<List<AyGroupBaseDto>> selectGroupByIds(@RequestBody List<Integer> groupIds) {
        return ResponseResult.ok(ayGroupBaseService.selectGroupByIds(groupIds));
    }
}
